/* Copyright (C) 2010 Luca Piccinelli
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* 
 * File:   cvMat_formatters.cpp
 * Author: picci
 *
 * Created on November 27, 2010, 4:31 PM
 */

#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <boost/any.hpp>
#include <exception>

#include "formatters/formatters.h"
#include "formatters/cvMat_formatters.h"
#include "pipeline/pipeline_io.h"

namespace NAMESPACE{

// ****** CvMatHgWndFormatter implementation ***********************************

const std::string CvMatHgWndFormatter::DEFAULT_TITLE = "Untitled";

//  _____________________________________________________
// |   Standard constructors                             |
// |_____________________________________________________|
    
CvMatHgWndFormatter::~CvMatHgWndFormatter(){
    cvDestroyWindow(title.c_str());
}
CvMatHgWndFormatter::CvMatHgWndFormatter() : AbstractFormatter(), title(DEFAULT_TITLE) {}
CvMatHgWndFormatter::CvMatHgWndFormatter(const CvMatHgWndFormatter& f) :  AbstractFormatter(f),
                                                                          title(f.title){}
CvMatHgWndFormatter& CvMatHgWndFormatter::operator=(const CvMatHgWndFormatter& f){
    if(this == &f) return *this;
    AbstractFormatter::operator =(f);
    title = f.title;
    return *this;
}

CvMatHgWndFormatter::CvMatHgWndFormatter(const IOKey& k, std::string _title) : AbstractFormatter(k),
                                                                               title(_title){}

CvMatHgWndFormatter::CvMatHgWndFormatter(std::string _title) : AbstractFormatter(),
                                                               title(_title){}

CvMatHgWndFormatter& CvMatHgWndFormatter::set_wnd_title(const std::string& _title){
    title = _title;
    return *this;
}

Formatter& CvMatHgWndFormatter::format() const{ throw std::runtime_error("Not implemented"); }
Formatter& CvMatHgWndFormatter::format(const IOElement& el) const{
    cv::Mat* m = boost::any_cast<cv::Mat*>(el.getElement());
    cv::Mat* m_show_ptr = m;
    cv::Mat  m_show;
    if(m->type() == cv::DataType<double>::type){
        double max = *std::max_element(m->begin<double>(), m->end<double>());
        m->convertTo(m_show, cv::DataType<uchar>::type, 255.0 / max);
        m_show_ptr = &m_show;
    }
    cv::imshow(title, *m_show_ptr);

    return *(const_cast<CvMatHgWndFormatter*>(this));
}

// ......................................................

// -----------------------------------------------------------------------------

}
